import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/login.vue'
import Register from '../components/register'
import header from '../components/headers'
import home from '../components/home'
import UserInfo from '../components/userinfo'
import ChangePwd from '../components/changePwd'
import Address from '../components/address'
import Order from '../components/order'
import Product from '../components/product'
import Ahome from '../components/admin/home'
import AllUser from '../components/admin/allUser'
import AllOrder from '../components/admin/allOrder'
import AllProduct from '../components/admin/allProduct'
import Inform from '../components/admin/inform'
import ProductDetail from '../components/productDetail'
import Car from '../components/car'

Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  routes: [
    {
      path: '/header',
      component: header,
      children: [
        {
          path: '/login',
          component: Login
        },
        {
          path: '/register',
          component: Register
        },

      ]
    },
    {
      path: '/',
      component: home
    },
    {
      path: '/home',
      component: home,
      children: [
        {
          path: '/userinfo',
          component: UserInfo
        },
        {
          path: '/changePwd',
          component: ChangePwd
        },
        {
          path: '/address',
          component: Address
        },
        {
          path: '/car',
          component: Car
        },
        {
          path: '/order',
          component: Order
        },
        {
          path: '/product',
          component: Product
        },
        {
          path: '/productDetail',
          component: ProductDetail
        },
      ]
    },
    {
      path: '/admin',
      component: Ahome,
      redirect: '/allUser',
      children: [

        {
          path: '/allOrder',
          component: AllOrder
        },
        {
          path: '/allUser',
          component: AllUser
        },
        {
          path: '/allProduct',
          component: AllProduct
        },
        {
          path: '/inform',
          component: Inform
        },

      ]
    }

  ]
});

router.beforeEach((to, from, next) => {

  if (to.path == '/login' || to.path == '/register' || to.path == '/admin') {
    return next();
  }

  const tokenStr = window.sessionStorage.getItem('user');
  if (!tokenStr) {
    return next('/login')
  }
  next();

});

export default router
